package edu.qust.y22.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.qust.y22.bean.Ticket;
import edu.qust.y22.bean.TicketExample;
import edu.qust.y22.bean.WebModel;
import edu.qust.y22.mapper.TicketMapper;
import edu.qust.y22.service.TicketService;

@Service
public class TicketServiceImpl implements TicketService{
	
	@Autowired
	private TicketMapper ticketMapper;
	
	@Override
	public List<Ticket> selectTicket3(String start,String end,String startTime) {
		TicketExample be = new 	TicketExample();
		TicketExample.Criteria c = be.createCriteria();
		c.andStartEqualTo(start);
		c.andEndEqualTo(end);
		c.andStartTimeEqualTo(startTime);
		
		List<Ticket> ticketList = this.ticketMapper.selectByExample(be);
		
		return ticketList;
	}

	@Override
	public List<Ticket> selectTicket2(String start, String end) {
		TicketExample be = new 	TicketExample();
		TicketExample.Criteria c = be.createCriteria();
		c.andStartEqualTo(start);
		c.andEndEqualTo(end);
		
		List<Ticket> ticketList = this.ticketMapper.selectByExample(be);
		return ticketList;
	}

	@Override
	public List<Ticket> selectTicket1(String start) {
		TicketExample be = new 	TicketExample();
		TicketExample.Criteria c = be.createCriteria();
		c.andStartEqualTo(start);
		
		List<Ticket> ticketList = this.ticketMapper.selectByExample(be);
		return ticketList;
	}

	@Override
	public List<Ticket> selectTicket() {
		TicketExample be = new 	TicketExample();
		
		List<Ticket> ticketList = this.ticketMapper.selectByExample(be);
		return ticketList;
	}

	@Override
	public boolean updateTicketSum(Ticket ticket) {
		int sum = this.ticketMapper.updateByPrimaryKey(ticket);
		if(sum>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean deleteTicket(int id) {
		
		int success = this.ticketMapper.deleteByPrimaryKey(id);
		if(success>0) {
			return true;
		}
		return false;
	}

	@Override
	public void selectAll(WebModel wm) {
		
		TicketExample be = new TicketExample();
		be.setLimitStart(wm.getLimitStart());
		be.setPageSize(wm.getPs());
		
		List<Ticket> tickets = this.ticketMapper.selectByExample(be);
		int count = this.ticketMapper.countByExample(be);
		
		wm.setData(tickets);
		wm.setTotalCount(count);
	}

	@Override
	public boolean addticket(Ticket ticket) {
		
		int sc = this.ticketMapper.insert(ticket);
		if(sc>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateTicket(Ticket ticket) {
		
		int sc = this.ticketMapper.updateByPrimaryKey(ticket);
		if(sc>0) {
			return true;
		}
		return false;
	}

}
